#include "qtc_alarm_rest/AlarmHistoryClient.h"
#include <QNetworkRequest>
#include <QUrl>
#include <QDebug>

namespace qtc_alarm_rest {

AlarmHistoryClient::AlarmHistoryClient(const Config& cfg, QObject* parent)
  : QObject(parent), m_cfg(cfg) {}

void AlarmHistoryClient::search(const QUrlQuery& params) {
  QUrl url(m_cfg.service_uri + "/search/alarm");
  url.setQuery(params);
  QNetworkRequest req(url);
  auto* reply = m_net.get(req);
  QObject::connect(reply, &QNetworkReply::finished, this, [this, reply]() {
    if (reply->error() != QNetworkReply::NoError) {
      emit error(reply->errorString());
    } else {
      emit resultReceived(reply->readAll());
    }
    reply->deleteLater();
  });
}

} // namespace qtc_alarm_rest